home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbcons1a / frmconso.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-09-24  |  10.0 KB  |  263 lines

  1. VERSION 5.00
  2. Begin VB.Form frmConsole 
  3.    BackColor       =   &H80000004&
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "VBConsole"
  6.    ClientHeight    =   6630
  7.    ClientLeft      =   45
  8.    ClientTop       =   330
  9.    ClientWidth     =   10290
  10.    BeginProperty Font 
  11.       Name            =   "Fixedsys"
  12.       Size            =   9
  13.       Charset         =   0
  14.       Weight          =   400
  15.       Underline       =   0   'False
  16.       Italic          =   0   'False
  17.       Strikethrough   =   0   'False
  18.    EndProperty
  19.    LinkTopic       =   "Form1"
  20.    MaxButton       =   0   'False
  21.    MinButton       =   0   'False
  22.    ScaleHeight     =   6630
  23.    ScaleWidth      =   10290
  24.    StartUpPosition =   3  'Windows Default
  25.    Begin VB.PictureBox picConsole 
  26.       Align           =   1  'Align Top
  27.       Height          =   6825
  28.       Left            =   0
  29.       ScaleHeight     =   6765
  30.       ScaleWidth      =   10230
  31.       TabIndex        =   0
  32.       Top             =   0
  33.       Width           =   10290
  34.       Begin VB.CommandButton cmdMin 
  35.          Caption         =   "<"
  36.          Height          =   225
  37.          Left            =   9450
  38.          TabIndex        =   2
  39.          Top             =   6330
  40.          Width           =   255
  41.       End
  42.       Begin VB.ListBox lstConsole 
  43.          BackColor       =   &H00008080&
  44.          ForeColor       =   &H00C0FFFF&
  45.          Height          =   6585
  46.          ItemData        =   "frmConsole.frx":0000
  47.          Left            =   0
  48.          List            =   "frmConsole.frx":0007
  49.          MousePointer    =   3  'I-Beam
  50.          TabIndex        =   4
  51.          Top             =   0
  52.          Width           =   9975
  53.       End
  54.       Begin VB.CommandButton cmdColorCycle 
  55.          Caption         =   "COLOR"
  56.          Height          =   1575
  57.          Left            =   9960
  58.          TabIndex        =   3
  59.          Top             =   0
  60.          Width           =   255
  61.       End
  62.       Begin VB.CommandButton cmdCLS 
  63.          Caption         =   "CLEAR"
  64.          Height          =   1215
  65.          Left            =   9960
  66.          TabIndex        =   1
  67.          Top             =   5400
  68.          Width           =   255
  69.       End
  70.    End
  71. Attribute VB_Name = "frmConsole"
  72. Attribute VB_GlobalNameSpace = False
  73. Attribute VB_Creatable = False
  74. Attribute VB_PredeclaredId = True
  75. Attribute VB_Exposed = False
  76. 'The Simple to use VB Console
  77. 'Author: Zane Horton
  78. 'Company: Camelback Research Alliance
  79. 'This is the console form. You can add it to any project with just this form.
  80. 'All of the code for it is self contained, and it uses no API calls or special controls.
  81. 'All output to this console is placed on its own line.
  82. 'If you write a string longer then the standard width of the console
  83. '(as declared by intConsoleTextWidth), it will hard wrap it (I.E. it breaks
  84. 'words between lines).
  85. 'To output a line to the console, do this (from another form):
  86. 'frmconsole.cout("Some string with a user value of :" + MyUserValue)
  87. 'You can clear the Console with frmConsole.cmdCLS_Click
  88. 'You can cycle the colors with frmConsole.cmdColorCycle_Click
  89. 'You can scroll the console with the following sub calls (self explanatory)
  90. 'frmConsole.ScrollToBeginning
  91. 'frmConsole.ScrollUp
  92. 'frmConsole.ScrollDown
  93. 'frmConsole.ScrollToEnd
  94. 'You can also toggle the Console tools with frmConsole.cmdMin_Click
  95. 'Note - You can add all the text to the console you want while it's hidden,
  96. 'But don't try to scroll the console while it's hidden - it's just extra CPU
  97. 'Time wasted (since I made it robust enough to not die when you try that)
  98. 'Yes I know that the text box could be used, but I liked the list box's usability
  99. 'much better (the text box is just too annoying since you can't tell it not to
  100. 'auto-wrap the text, which screwed up all my calculations...)
  101. 'Note - Clear the console every once in a while, since you can only have up to
  102. '32000 lines. (After that it goes on a coffee break and ignores you trying to
  103. 'add lines...)
  104. 'If you want to output the contents of the console to a file, do:
  105. 'frmConsole.SaveContents(strFileName)
  106. Const intConsoleTextWidth As Integer = 80
  107. Const intConsoleWidthMin As Integer = 10110
  108. Const intConsoleWidthMax As Integer = 10380
  109. Const NumOfColorSchemes As Byte = 5
  110. Const ForceRefresh As Boolean = True
  111. Dim CurrentColorScheme As Byte
  112. Dim ConsoleBackColor(1 To NumOfColorSchemes) As Variant
  113. Dim ConsoleForeColor(1 To NumOfColorSchemes) As Variant
  114. Dim FormSmall As Boolean
  115. Dim i, j As Integer
  116. Option Explicit
  117. Sub cmdCLS_Click()
  118.     lstConsole.Clear
  119. End Sub
  120. Sub cmdColorCycle_Click()
  121.     If CurrentColorScheme = NumOfColorSchemes Then
  122.         CurrentColorScheme = 1
  123.       Else
  124.         CurrentColorScheme = CurrentColorScheme + 1
  125.     End If
  126.     lstConsole.BackColor = ConsoleBackColor(CurrentColorScheme)
  127.     lstConsole.ForeColor = ConsoleForeColor(CurrentColorScheme)
  128.     'This is optional
  129.     'Cout ("Current color scheme number " + Format(CurrentColorScheme))
  130. End Sub
  131. Sub cmdMin_Click()
  132.     If FormSmall Then
  133.         For i = intConsoleWidthMin To intConsoleWidthMax
  134.             frmConsole.Width = i
  135.         Next i
  136.         FormSmall = False
  137.         cmdMin.Caption = "<"
  138.       Else
  139.         For i = intConsoleWidthMax To intConsoleWidthMin Step -1
  140.             frmConsole.Width = i
  141.         Next i
  142.         FormSmall = True
  143.         cmdMin.Caption = ">"
  144.     End If
  145. End Sub
  146. Private Sub Form_Load()
  147.     lstConsole.Clear
  148.     FormSmall = False
  149.     CurrentColorScheme = 0
  150.     'If you want to add more color schemes, go right ahead!
  151.     'Just make sure to change the constant up top called
  152.     'NumOfColorSchemes. You can have up to 255 color schemes!
  153.     'If you're daring, make a RANDOM color scheme like this:
  154.     'ConsoleBackColor(6) = rgb((int(rnd(1)*254)+1),(int(rnd(1)*254)+1),(int(rnd(1)*254)+1))
  155.     'ConsoleForeColor(6) = rgb((int(rnd(1)*254)+1),(int(rnd(1)*254)+1),(int(rnd(1)*254)+1))
  156.     'White text on black
  157.     ConsoleBackColor(1) = vbBlack
  158.     ConsoleForeColor(1) = vbWhite
  159.     'Black text on White
  160.     ConsoleBackColor(2) = vbWhite
  161.     ConsoleForeColor(2) = vbBlack
  162.     'Monochrome style
  163.     ConsoleBackColor(3) = vbBlack
  164.     'ConsoleBackColor(3) = &H8000&   'An alternate for light green text on dark green
  165.     ConsoleForeColor(3) = &HFF00&
  166.     'Amber style
  167.     ConsoleBackColor(4) = vbBlack
  168.     'ConsoleBackColor(4) = &H8080&   'An alternate for light Amber text on dark Amber
  169.     ConsoleForeColor(4) = &HC0FFFF
  170.     'Commodore style
  171.     ConsoleBackColor(5) = &HFF0000
  172.     ConsoleForeColor(5) = &HFFFF00
  173.     'Random  :)
  174.     'ConsoleBackColor(6) = RGB((Int(Rnd(1) * 254) + 1), (Int(Rnd(1) * 254) + 1), (Int(Rnd(1) * 254) + 1))
  175.     'ConsoleForeColor(6) = RGB((Int(Rnd(1) * 254) + 1), (Int(Rnd(1) * 254) + 1), (Int(Rnd(1) * 254) + 1))
  176.     Call cmdColorCycle_Click
  177. End Sub
  178. Sub Cout(text As String)
  179.     Dim TextLength As Integer
  180.     Dim LastLineLen As Integer
  181.     Dim NumLines As Byte
  182.     Dim k As Byte
  183.     If lstConsole.ListCount > 32000 Then
  184.         Exit Sub
  185.     End If
  186.     TextLength = Len(text)
  187.     If TextLength <= intConsoleTextWidth Then
  188.         'This string is less than the normal line size, so just add it.
  189.         lstConsole.AddItem text
  190.       Else
  191.         'This string is greater than the normal line size, so make sure it displays properly.
  192.         
  193.         'Set the length of the last line's text
  194.         LastLineLen = TextLength Mod intConsoleTextWidth
  195.         'Set the number of full lines to output
  196.         NumLines = Int(TextLength / intConsoleTextWidth)
  197.         'Output each line (except the partial last line if applicable
  198.         For k = 1 To NumLines
  199.             lstConsole.AddItem Mid$(text, (((k - 1) * intConsoleTextWidth) + 1), intConsoleTextWidth)
  200.         Next k
  201.         'Output the last line (if applicable)
  202.         If LastLineLen > 0 Then lstConsole.AddItem Right(text, LastLineLen)
  203.     End If
  204.     If ForceRefresh Then lstConsole.Refresh
  205. End Sub
  206. Private Sub lstConsole_DblClick()
  207.     Clipboard.SetText lstConsole.List(lstConsole.ListIndex)
  208. End Sub
  209. Sub ScrollDown()
  210.     'OK, so I cheated, but hey, It works!
  211.     lstConsole.SetFocus
  212.     SendKeys "{DOWN}"   'Send the DOWN arrow key to the list
  213. End Sub
  214. Sub ScrollUp()
  215.     lstConsole.SetFocus
  216.     SendKeys "{UP}"     'Send the UP arrow key to the list
  217. End Sub
  218. Sub ScrollToEnd()
  219.     lstConsole.SetFocus
  220.     SendKeys "^{END}"   'Send the CTRL + END keys to the list
  221. End Sub
  222. Sub ScrollToBeginning()
  223.     lstConsole.SetFocus
  224.     SendKeys "^{HOME}"  'Send the CTRL + HOME keys to the list
  225. End Sub
  226. Sub SaveContents(strFileName As String)
  227.     'This function will output the console's contents (if there are any) to a
  228.     'Text file as specified in the passed string.
  229.     Dim ListLine As Integer
  230.     Dim FreeFileNum As Byte
  231.     On Error GoTo BadSave
  232.     'Validate the file name that got passed
  233.     If Mid(strFileName, 2, 2) <> ":\" Then
  234.         MsgBox$ ("Please Enter a valid filename, Like C:\ConsoleOutput.txt")
  235.         Exit Sub
  236.     End If
  237.     'Make sure there are some contents to save first
  238.     If lstConsole.ListCount = 0 Then
  239.         MsgBox$ ("Please wait until there is output before you try to save it.")
  240.         Exit Sub
  241.     End If
  242.     'Now we Actually save the file
  243.     'Get a free file number
  244.     FreeFileNum = FreeFile
  245.     'Open the file
  246.     Open strFileName For Output As #FreeFileNum
  247.     'Loop for each list line
  248.     For ListLine = 1 To lstConsole.ListCount
  249.         Print #FreeFileNum, lstConsole.List(ListLine)
  250.     Next ListLine
  251.     'Close the file
  252.     Close #FreeFileNum
  253.     'I always like to close all the open files just in case :)
  254.     Close
  255.     'Tell the user that it's all A-OK.
  256.     MsgBox$ ("The Contents have been saved to " + strFileName + ".")
  257.     'Exit to make sure we don't tell the user it errored after it saved OK :)
  258.     Exit Sub
  259. BadSave:
  260.     'Uh-Oh, something happened
  261.     MsgBox$ ("There was an error saving the contents to " + strFileName + ".")
  262. End Sub
  263.